import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.mxgraph.*;
import com.mxgraph.model.mxCell;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class MapPanel extends JPanel {
	private Image img;
	private Game g;
	private Object[] edgeList;
	private Object[] vertices;
	private JButton close;
	private boolean atLandmark;
	private int graphLoc;
	
	/**
	 * Create the panel.
	 */
	public MapPanel(Game g, String img) {
		this(g, new ImageIcon(img).getImage());
	}
	
	/**
	 * @wbp.parser.constructor
	 */
	public MapPanel(Game g, Image img)
	{
		setBorder(new EmptyBorder(5, 5, 5, 5));
		
		atLandmark = true;
		graphLoc = 0;
		this.g = g;
		this.img = img;
		Dimension size = new Dimension(800, 600);
		setPreferredSize(size);
		setMinimumSize(size);
		setMaximumSize(size);
		
		mxGraph graph = new mxGraph();
		mxCell parent = (mxCell) graph.getDefaultParent();
		
		graph.getModel().beginUpdate();
		try
		{
			Object[] vertices = {graph.insertVertex(parent,  null, Map.Landmarks[0].getName(), 780, 230, 10, 10), graph.insertVertex(parent, null, Map.Landmarks[1].getName(), 710, 250, 10, 10), graph.insertVertex(parent, null, Map.Landmarks[2].getName(), 680, 200, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[3].getName(), 600, 170, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[4].getName(), 550, 190, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[5].getName(), 490, 230, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[6].getName(), 375, 320, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[7].getName(), 315, 380, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[8].getName(), 290, 300, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[9].getName(), 280, 260, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[10].getName(), 250, 220, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[11].getName(), 200, 250, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[12].getName(), 160, 200, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[13].getName(), 110, 110, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[14].getName(), 75, 180, 10, 10), graph.insertVertex(parent,  null, Map.Landmarks[15].getName(), 20, 50, 10, 10)};
			Object[] edgeList = {graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[1]), vertices[0], vertices[1]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[2]), vertices[1], vertices[2]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[3]), vertices[2], vertices[3]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[4]), vertices[3], vertices[4]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[5]), vertices[4], vertices[5]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[6]), vertices[5], vertices[6]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[7]), vertices[6], vertices[7]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[8]), vertices[7], vertices[8]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[9]), vertices[8], vertices[9]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[10]), vertices[9], vertices[10]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[11]), vertices[10], vertices[11]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[12]), vertices[11], vertices[12]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[13]), vertices[12], vertices[13]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[14]), vertices[13], vertices[14]), graph.insertEdge(parent, null, Integer.toString(Map.Landmark_Locations[15]), vertices[14], vertices[15])};
		}
		finally
		{
			graph.getModel().endUpdate();
		}
		
		graph.setCellsEditable(false);
		graph.setVertexLabelsMovable(false);
		
		close = new JButton("Close");
		close.setBounds(569, 451, 158, 60);
		close.addActionListener(new CloseButtonListener());
		add(close);
		
		mxGraphComponent graphComponent = new mxGraphComponent(graph);
		graphComponent.setEnabled(false);
		graphComponent.getGraph().setCellsEditable(false);
		add(graphComponent);
	}
	
	public void paintComponent(Graphics g)
	{
		super.paintComponent(g);

		g.drawImage(img, 800, 600, null);
	}

	public static void main(String[] args)
	{
		JFrame frame = new JFrame("Test");
		MapPanel map = new MapPanel(new Game(), "/resources/us_outline.jpg");
		//map.setVisible(true);
		frame.getContentPane().add(map);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 600);
		frame.pack();
		frame.setVisible(true);
	}
	
	public void update(boolean atLandmark)
	{
		/*if(!this.atLandmark)
		{
			if(atLandmark)
			{	
				graphLoc++;
				((mxCell) (vertices[graphLoc/2])).setStyle("defaultVertex;fillColor=yellow");
				((mxCell)edgeList[(graphLoc - 1)/2]).setStyle("defaultVertex");
			}
		}
		else
		{
			if(!atLandmark)
			{
				graphLoc++;
				((mxCell) (vertices[graphLoc/2])).setStyle("defaultVertex;fillColor=yellow");
				((mxCell)edgeList[(graphLoc - 1)/2]).setStyle("defaultVertex");				
			}
		} */
		this.atLandmark = atLandmark;
	}

	
	class CloseButtonListener implements ActionListener
	{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			g.changePanel("TurnPanel");
		}
	}
}
